const fs = require('fs');
const path = require('path');
const XLSX = require('xlsx');


function filterWorkBoox(workbook) {
    const sheets = workbook.Sheets;
    const filterSheetsNames = workbook.Workbook.Sheets.filter(item =>
        item.Hidden === 0).map(it => it.name);

    return Object.entries(sheets).filter(sheetList =>
        filterSheetsNames.indexOf(sheetList[0]) !== -1)
        .map(sheet => {
            const [sheetName, sheetData] = sheet;
            return {
                sheetName,
                sheetData
            };
        });
}

function readSyncXlsx() {
    const testpath = path.resolve(__dirname, '../source/test.xlsx')
    const data = fs.readFileSync(testpath);
    const workbook = XLSX.read(data, { type: 'buffer' });

    // console.log('SheetNames', workbook.SheetNames);
    // console.log('Names', workbook.Workbook.Sheets);
    // console.log('Sheets', workbook.Sheets['8.制度诊断']);
    const sheets = filterWorkBoox(workbook);
    console.log(sheets[0].sheetData);
    // console.log(sheets[0].sheetData['!merges']);

    return {
        workbook,
        sheets
    };
}

module.exports = { readSyncXlsx };
